<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){
    	// layout('header');
    	// //获取随机一条人类验证
    	$question = D('verify')->getOneVerify();
    	$this->assign('question',$question);
        $this->assign('title',"登录");
        $this->display();
    }

    public function checkdata()
    {
    	$verify = I('post.verify');

        // 判断人类验证是否正确
    	if(md5($verify) != $_SESSION['answer']){
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证不通过'
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	if($res = D('user')->checkLogin(I('post.email'),I('post.password'))){

            if($res['stop']==1){
                $arr = array(
                    'error'=>0,
                    'info'=>'你被停用了，请联系管理员'
                    );
                echo json_encode($arr);
                exit;
            }

    		$_SESSION['id']=$res['id'];
    		$_SESSION['nickname']=$res['nickname'];
    		$_SESSION['face']=$res['face'];

    		$arr = array(
    			'error'=>0,
    			'info'=>'登录成功'
    			);
    		echo json_encode($arr);
    		exit;
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'邮箱或者密码错误'
    			);
    		echo json_encode($arr);
    		exit;
    	}


    }

    public function loginout()
    {
    	session_unset();
    	session_destroy();

    	$this->success('退出成功',U('home/index/index'),0);

    }
    /**
     * [qqreturn 回调]
     * @Author   Xuebingsi
     * @DateTime 2017-04-08T10:43:05+0800
     * @return   [type]                   [description]
     */
    public function qqreturn()
    {
        header("Content-type: text/html; charset=utf-8");


        $openid=getOpenid();

        echo $openid;

        if($res = M('user')->where(array('openid'=>$openid))->find()){
            $_SESSION['id']=$res['id'];
            $_SESSION['nickname']=$res['nickname'];
            $_SESSION['face']=$res['face'];
            $this->success('登录成功',U('home/index/index'));
        }else{

            echo "sdfsdfsdf";

            $userInfo = getUserInfo();

            // $data = file_get_contents($userInfo['figureurl_2']);

            // file_put_contents('./Upload/qqface/sdflsdfjl.jpg', $data);

            // echo "787878";
            
            $data['nickname']= $userInfo['nickname'];
            $data['openid']= $openid;
            $data['city']= $userInfo['city'];
            $data['create_time']= time();

            if($res = M('user')->add($data)){
                $_SESSION['id']=$res;
                $_SESSION['nickname']=$data['nickname'];
                // $_SESSION['face']=$res['face'];
                $this->success('登录成功',U('home/index/index'));
            }
        }
    }

    /**
     * [qqlogin qq登录]
     * @Author   Xuebingsi
     * @DateTime 2017-04-08T10:43:22+0800
     * @return   [type]                   [description]
     */
    public function qqlogin()
    {
        // require_once("../../API/qqConnectAPI.php");
        // 
        xbslogin();

        

    }
}